package com.suning.app.snpaasadmin.util;

import com.suning.app.snpaasadmin.domain.OauthToken;
import com.suning.app.snpaasadmin.exception.CloudFoundryApiException;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpRequestBase;

/**
 * @author lmx
 *
 */
public class CloudFoundryClientUtil {

    public static void setAccessHeaders(HttpRequestBase method, OauthToken token) throws Exception {
        method.addHeader(HttpHeaders.AUTHORIZATION, token.getTokenType() + " " + token.getAccessToken());
        method.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        method.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    }

    public static void setUuaHeaders(HttpRequestBase method, OauthToken token) throws Exception {
        method.addHeader(HttpHeaders.AUTHORIZATION, token.getTokenType() + " " + token.getAccessToken());
        method.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        method.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    }

    private static Map<String, String> buildCcAccessHeaderParams(OauthToken token) throws Exception {
        Map<String, String> results = new HashMap<String, String>();
        results.put(HttpHeaders.AUTHORIZATION, token.getTokenType() + " " + token.getAccessToken());
        results.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        results.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        return results;
    }

    public static String doOauth(String url, Map<String, String> params, String host) throws Exception {
        Base64 base64 = new Base64();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(HttpHeaders.HOST, host);
        headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        headers.put("Authorization", String.format("Basic %s",
                new String(base64.encode(String.format("%s:%s", "cf", "").getBytes("UTF-8")), "UTF-8")));

        HttpClientResponse response = HttpClientUtil.doPostWithCC(url, params, headers);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()),
                    (String) data.get("error_description"));
        }
        return response.getBody();
    }

    public static String doCCPost(String url, Map<String, String> params, OauthToken token) throws Exception {
        Map<String, String> headers = buildCcAccessHeaderParams(token);
        HttpClientResponse response = HttpClientUtil.doPostWithJsonType(url, params, headers);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()), (String) data.get("description"));
        }
        return response.getBody();
    }

    public static String doCCPut(String url, Map<String, Object> params, OauthToken token) throws Exception {
        Map<String, String> headers = buildCcAccessHeaderParams(token);
        HttpClientResponse response = HttpClientUtil.doPut(url, params, headers);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()), (String) data.get("description"));
        }
        return response.getBody();
    }

    public static String doCCGet(String url, Map<String, String> params, OauthToken token) throws Exception {
        Map<String, String> headers = buildCcAccessHeaderParams(token);
        HttpClientResponse response = HttpClientUtil.doGet(url, params, headers);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            if (data.get("error") != null) {
                throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()), (String) data.get("error"));
            }
            throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()), (String) data.get("description"));
        }
        return response.getBody();
    }

    public static void doCCDelete(String url, Map<String, String> params, OauthToken token) throws Exception {
        Map<String, String> headers = buildCcAccessHeaderParams(token);
        HttpClientResponse response = HttpClientUtil.doDelete(url, params, headers);
        if (!HttpStatusUtil.isSuccessStatus(response.getStatus())) {
            String content = response.getBody();
            Map<String, Object> data = JsonUtil.convertJsonToMap(content);
            throw new CloudFoundryApiException(Integer.valueOf(response.getStatus()), (String) data.get("description"));
        }
    }
}
